﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using SadlandVillage.Minigames.Restaurant.Helper;
using SadlandVillage.Core.Helper;

namespace SadlandVillage.Minigames.Restaurant
{
    public class GameOver : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public GameOver(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public override void Draw(GameTime gameTime)
        {
            if (StaticContainer.platesXtotal.Count > 0)
            {
                GraphicsDevice graphics = GlobalContainer.graphics;
                graphics.Clear(Color.WhiteSmoke);

                SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
                SpriteFont font = (SpriteFont)Game.Services.GetService(typeof(SpriteFont));

                int totalPoints = 0;
                int totalMatchedPoints = 0;

                sBatch.Begin();

                int y = 0;

                Dictionary<string, Color> partialResults = new Dictionary<string, Color>();

                foreach (KeyValuePair<Plates, Pair<int>> actual in StaticContainer.platesXtotal)
                {
                    int actualTotalPoints = actual.Value.head;
                    int actualMatchedPoints = actual.Value.tail;

                    totalPoints += actualTotalPoints;
                    totalMatchedPoints += actualMatchedPoints;

                    int actualPercent = (actualMatchedPoints * 100) / actualTotalPoints;
                    Color actualTextColor;

                    if (actualPercent >= 80)
                    {
                        actualTextColor = Color.DarkGreen;
                    }
                    else
                    {
                        actualTextColor = Color.DarkRed;
                    }

                    partialResults.Add(PlatesHelper.GetPlateName(actual.Key) + " - " + actualPercent + "% completed", actualTextColor);
                    //sBatch.DrawString(font, , new Vector2(20, y += 30), actualTextColor);
                }

                int totalPercent = ((totalMatchedPoints * 100) / totalPoints);
                bool success;
                Color totalTextColor;

                if (totalPercent >= 80)
                {
                    totalTextColor = Color.DarkGreen;
                    sBatch.Draw(StaticContainer.gameOverWin, Vector2.Zero, Color.White);
                    success = true;
                }
                else
                {
                    totalTextColor = Color.DarkRed;
                    sBatch.Draw(StaticContainer.gameOverLose, Vector2.Zero, Color.White);
                    success = false;
                }

                foreach (KeyValuePair<string, Color> actual in partialResults)
                {
                    StringEffects.PrintShadowedString(sBatch, font, actual.Key, new Vector2(30, y+=30), actual.Value, Color.White, 1);
                }

                StringEffects.PrintShadowedString(sBatch, font, "Total: " + totalPercent + "% completed", new Vector2(30, y + 60), totalTextColor, Color.Yellow, 1);

                if (success)
                {
                    StringEffects.PrintShadowedString(sBatch, font, "Press \"Enter\" to play again, or \"Esc\" to leave the restaurant.", new Vector2(30, 550), Color.Navy, Color.White, 1);
                }
                else
                {
                    StringEffects.PrintShadowedString(sBatch, font, "Press \"Enter\" to try again, or \"Esc\" to give up.", new Vector2(30, 550), Color.Navy, Color.White, 1);
                }

                sBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}
